program FairSquareMedium;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  math,
  BigInt in 'BigInt.pas',
  BInt in 'BInt.pas';

var
  f, g: TextFile;
  answer: Longint;

  a, b: number;


procedure ReadData;
begin
  readFromFile(f, a, b);
  //Readln(f, a, b);
end;

function IsPalindrome(s: String): Boolean; overload;
var
  i, len: byte;
begin
  len := Length(s);
  if len < 2 then begin
    Result := True;
    Exit;
  end;

  for i := 1 to len shr 1 do
    if s[i] <> s[len + 1 - i] then begin
      Result := False;
      Exit;
    end;
  Result := True;
end;

function IsPalindrome(i: Int64): Boolean; overload;
begin
  Result := IsPalindrome(IntToStr(i));
end;

procedure MakePalindrome(var a:Int64);
var
  i, leng: byte;
  s: String;
begin
  s := IntToStr(a);
  leng := Length(s);
  for i := leng downto (leng shr 1) + 1 do
    s[i] := s[leng + 1 - i];
  a := StrToInt(s);
end;

function NextPalindrome(var i: Int64): Int64;
var
  j, x: Int64;
  leng: byte;
  s: String;
  k: Byte;
begin
  j := i;
  MakePalindrome(j);
  if j > i then begin
    Result := j;
    Exit;
  end;
  repeat
    s := IntToStr(j);
    leng := Length(s);
    x := 1;
    for k := 1 to (leng shr 1) do
      x := x * 10;
    j := j + x;
    MakePalindrome(j);
  until j > i;
  Result := j;
end;

procedure ProcessData;
var
  k, fl: Int64;
begin
  k := ceil(sqrt(a));
  fl := trunc(sqrt(b));
  repeat
    if IsPalindrome(Sqr(k)) then
      Inc(Answer);
    k := NextPalindrome(k);
  until k > fl;
end;


procedure OutputVerdict(casenum: Word);
begin
  writeln(g, 'Case #', casenum, ': ', answer);
end;

var
  i, t: Word;
  s: String;
begin
  try
    s := ParamStr(1);
    if (s = '') then
       s := 'inputmedium.txt';
    AssignFile(f, s);
    Reset(f);
    AssignFile(g, 'outputmedium.txt');
    Rewrite(g);
    try
      Readln(f, t);
      for i := 1 to t do begin
        answer := 0;
        ReadData;
        ProcessData;
        OutputVerdict(i);
      end;
    finally
      CloseFile(f);
      CloseFile(g);
    end;
    { TODO -oUser -cConsole Main : Insert code here }
  except
    on E: Exception do begin
      Writeln(E.ClassName, ': ', E.Message);
      readln;
    end;
  end;
end.
